<?php
if(!defined('IN_SING')) {
	exit('Access Denied');
}

class dbt_public_list extends sing_table{
	
	public function fetch_all_by_sql($sql, $OrderArr = array(), $start = 0, $limit = 0, $count = 0, $alias = '') {
		if(!is_array($OrderArr)) {
			$OrderArr = '';
		}		
		if($count) {
			$sql_ext = substr($sql, (stripos($sql, 'from')+4),strlen($sql));
			if(stripos($sql_ext,'group by')!==false){
				$g = substr($sql_ext,stripos($sql_ext,'group by')+9,strlen($sql_ext));
				//var_dump($g);
				$f = stripos($g,' ')!==false?substr($g,0,stripos($g,' ')):$g;
				//$f = stripos($f,',')!==false?substr($f,0,stripos($g,',')):$f;
				$sql_ext = substr($sql_ext,0,stripos($sql_ext,'group by'));	
				//var_dump($f);			
				return DB::result_first('SELECT count(distinct '.$f.') FROM '.$sql_ext.' '.DB::limit($start, $limit), $OrderArr);
			}else	return DB::result_first('SELECT count(*) FROM '.$sql_ext.' '.DB::limit($start, $limit), $OrderArr);
		}
		return DB::fetch_all($sql.' '.DB::limit($start, $limit), $OrderArr);
	}
}
?>